feat(restore): add post-restore integrity validation#1170
Merged
Conversation
Add IntegrityCheckMode type with None/Quick/Full modes and a checkIntegrity() function that runs PRAGMA quick_check or integrity_check on restored databases. EnsureExists() now defaults to IntegrityCheckQuick so K8s init containers catch corrupt restores before the application starts. Adds -integrity-check CLI flag to the restore command for manual use. Closes #1164
PR Build Metrics
Binary Size
Dependency ChangesNo dependency changes. govulncheck OutputBuild Info
History (3 previous)
🤖 Updated on each push. |
fuchstim
approved these changes
Feb 26, 2026
- Add context.Context parameter to checkIntegrity() so cancellation/timeout is respected during long-running PRAGMAs - Use explicit switch on IntegrityCheckMode to reject unsupported values instead of silently falling through to quick_check
benbjohnson
approved these changes
Mar 4, 2026
Owner
benbjohnson
left a comment
There was a problem hiding this comment.
lgtm but there's a spot where it can be cleaned up a little.
Use QueryRowContext() instead of QueryContext() with row iteration since the integrity check returns a single result row.
…ellation - Validate IntegrityCheckMode in Restore() and RestoreV3() before doing any restore work, preventing invalid modes from causing unnecessary restore followed by deletion - Only delete restored DB on actual integrity failures, not on context cancellation or timeout, preserving valid restores interrupted by Ctrl+C or deadline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds optional integrity checking after database restores to catch corruption early, regardless of source.
Changes
IntegrityCheckModetype (None/Quick/Full): Controls whether and howPRAGMA integrity_checkorPRAGMA quick_checkruns after restore.checkIntegrity()function: Runs the selected integrity check on the restored database. On failure, cleans up the corrupt file and returns an error.Restore()andRestoreV3(): Both code paths now support post-restore validation.EnsureExists()defaults toIntegrityCheckQuick: K8s init containers callingEnsureExists()catch corrupt restores before the application starts.-integrity-checkCLI flag: Users can opt into validation vialitestream restore -integrity-check quick|full.Why
Even with the root cause fix for #1164 (PR #1166), integrity checking provides defense in depth against corruption from any source — disk errors, buggy storage backends, network corruption during transfer, etc. It's cheap insurance:
quick_checkis fast and catches most structural issues.Related to #1164
How Has This Been Tested?
go test -race -count=1 ./...— all tests passreplica_internal_test.go:TestCheckIntegrity_Quick_ValidDB— valid DB returns no errorTestCheckIntegrity_Full_ValidDB— full mode on valid DB returns no errorTestCheckIntegrity_None_Skips—IntegrityCheckNonereturns nil without opening DBTestCheckIntegrity_CorruptDB— corrupted DB returns integrity check errorTypes of changes
Checklist
go fmt,go vet)go test ./...)